<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入商品" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品金额类型" prop="orderType" v-if="queryParams.orderType == '10'? false : true">
        <el-select v-model="queryParams.orderType" clearable size="small">
          <el-option v-for="dict in dict.type.orderType" v-if="dict.value!=10" :key="dict.value" :label="dict.label" :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small">
          <el-option v-for="dict in dict.type.orderStatus" :key="dict.value" :label="dict.label" :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="配送方式" prop="deliveryType">
        <el-select v-model="queryParams.deliveryType" placeholder="请选择配送方式" clearable size="small">
          <el-option v-for="dict in dict.type.deliveryType" :key="dict.value" :label="dict.label" :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提货人/收货人姓名" prop="contactPerson">
        <el-input v-model="queryParams.contactPerson" placeholder="请输入提货人/收货人姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="提货人/收货人电话" prop="contactPhone">
        <el-input v-model="queryParams.contactPhone" placeholder="请输入提货人/收货人电话" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="快递公司" prop="deliveryFirm">
        <el-input v-model="queryParams.deliveryFirm" placeholder="请输入快递公司" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="快递单号" prop="oddNo">
        <el-input v-model="queryParams.oddNo" placeholder="请输入快递单号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-tabs v-model="tabsNames" @tab-click="handleTabClick">
      <el-tab-pane label="购买订单" name="buy"></el-tab-pane>
      <el-tab-pane label="兑换订单" name="exchange"></el-tab-pane>
    </el-tabs>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['pos:TOrderInfo:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['pos:TOrderInfo:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="TOrderInfoList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="订单编号" show-overflow-tooltip align="center" prop="orderNo" />
      <el-table-column label="商家" v-if="tabsNames=='exchange'" align="center" prop="userName" />
      <!--<el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="规格" align="center" prop="specification" /> -->
      <el-table-column label="商品" show-overflow-tooltip align="center" prop="goodsName" />
      <el-table-column label="订单类型" align="center" prop="orderType"  v-if="tabsNames=='exchange'">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.orderType" :value="scope.row.orderType" />
        </template>
      </el-table-column>
      <el-table-column label="支付金额" align="center" prop="payAmount" v-if="tabsNames=='buy'"/>
      <el-table-column label="兑换金额" align="center" prop="exchangeAmount" v-if="tabsNames=='exchange'"/>
      <el-table-column label="收货人姓名" width="150" align="center" prop="contactPerson" />
      <el-table-column label="收货人电话" width="150" align="center" prop="contactPhone" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.orderStatus" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="配送方式" align="center" prop="deliveryType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.deliveryType" :value="scope.row.deliveryType" />
        </template>
      </el-table-column>
<!--       <el-table-column label="商品数量" align="center" prop="goodsNum" />-->
<!--      <el-table-column label="物流地点/自提地点" width="150" show-overflow-tooltip align="center" prop="address" />
      <el-table-column label="配送费" align="center" prop="deliveryAmount" />
      <el-table-column label="自提" align="center" prop="shopName" />
      <el-table-column label="订单发货时间" align="center" prop="deliverGoodsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliverGoodsTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.payType" :value="scope.row.payType" />
        </template>
      </el-table-column>
      <el-table-column label="微信预支付时间" align="center" prop="prepayTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.prepayTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="兑换金额" align="center" prop="exchangeAmount" />
      <el-table-column label="微信预支付返回信息" align="center" prop="prepayInfo" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="快递公司" align="center" prop="deliveryFirm" />
      <el-table-column label="快递单号" show-overflow-tooltip align="center" prop="oddNo" />
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row)"
            v-hasPermi="['pos:TOrderInfo:edit']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDeliver(scope.row)"
            v-hasPermi="['pos:TOrderInfo:edit']" v-if = "scope.row.orderStatus == '20'? true : false">发货</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pos:TOrderInfo:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1180px" append-to-body>
      <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="150px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" disabled placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="用户" prop="userName">
          <el-input v-model="form.userName" disabled placeholder="请输入用户" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" disabled placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" disabled placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="商品" prop="goodsName">
          <el-input v-model="form.goodsName" disabled placeholder="请输入商品" />
        </el-form-item>
        <el-form-item label="商品数量" prop="goodsNum">
          <el-input v-model="form.goodsNum" disabled placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form.orderType" disabled style="width: 203px;" placeholder="请选择订单类型">
            <el-option v-for="dict in dict.type.orderType" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="form.orderStatus" disabled style="width: 203px;" placeholder="请选择订单类型">
            <el-option v-for="dict in dict.type.orderStatus" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配送方式" prop="deliveryType">
          <el-select v-model="form.deliveryType" disabled style="width: 203px;" placeholder="请选择配送方式">
            <el-option v-for="dict in dict.type.deliveryType" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物流地点/自提地点" prop="address">
          <el-input v-model="form.address" disabled placeholder="请输入物流地点/自提地点" />
        </el-form-item>
        <el-form-item label="配送费" prop="deliveryAmount">
          <el-input v-model="form.deliveryAmount" disabled placeholder="请输入配送费" />
        </el-form-item>
        <el-form-item label="提货人/收货人姓名" prop="contactPerson">
          <el-input v-model="form.contactPerson" disabled placeholder="请输入提货人/收货人姓名" />
        </el-form-item>
        <el-form-item label="自提" prop="shopName">
          <el-input v-model="form.shopName" disabled placeholder="请输入自提" />
        </el-form-item>
        <el-form-item label="提货人/收货人电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" disabled placeholder="请输入提货人/收货人电话" />
        </el-form-item>
        <el-form-item label="订单发货时间" prop="deliverGoodsTime">
          <el-date-picker clearable size="small" disabled style="width: 203px;" v-model="form.deliverGoodsTime" type="date"
            value-format="yyyy-MM-dd" placeholder="选择订单发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <el-select v-model="form.payType" disabled style="width: 203px;" placeholder="请选择支付方式">
            <el-option v-for="dict in dict.type.payType" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付金额" prop="payAmount"v-if="tabsNames=='buy'">
          <el-input v-model="form.payAmount" disabled placeholder="请输入支付金额" />
        </el-form-item>
<!--        <el-form-item label="微信预支付时间" prop="prepayTime">
          <el-date-picker clearable size="small" disabled style="width: 203px;" v-model="form.prepayTime" type="date"
            value-format="yyyy-MM-dd" placeholder="选择微信预支付时间">
          </el-date-picker>
        </el-form-item>-->
        <el-form-item label="兑换金额" prop="exchangeAmount" v-if="tabsNames=='exchange'">
          <el-input v-model="form.exchangeAmount" disabled placeholder="请输入兑换金额"  />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable size="small" disabled style="width: 203px;" v-model="form.payTime" type="date"
            value-format="yyyy-MM-dd" placeholder="选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="快递公司" prop="shipperName">
          <el-input v-model="form.shipperName" disabled placeholder="请输入快递公司" />
        </el-form-item>
        <el-form-item label="快递单号" prop="oddNo">
          <el-input v-model="form.oddNo" disabled placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input style="width: 930px;" disabled v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
<!--        <el-form-item label="微信预支付返回信息" prop="prepayInfo">
          <el-input style="width: 930px;" disabled v-model="form.prepayInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="发货" :visible.sync="deliverOpen" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="88px">
        <el-input v-model="form.brandId" placeholder="终端品牌" v-if="false"/>
        <el-form-item label="快递公司" prop="deliveryFirm">
          <el-select style="width: 100%;" v-model="form.deliveryFirm" filterable remote reserve-keyword
            placeholder="请输入关键词" :remote-method="getShipperCodeList" :loading="deliverLoading">
            <el-option v-for="item in shipperCodeList" :key="item.shipperCode" :label="item.shipperName"
              :value="item.shipperCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="oddNo">
          <el-input v-model="form.oddNo" placeholder="请输入快递单号" />
        </el-form-item>
<!--        <el-form-item v-if="form.brandId" label="SN码列表" prop="snCodes">
          <el-input v-model="form.snCodes" type="textarea" placeholder="请输入SN码,多个sn码请用 逗号 分割" />
        </el-form-item>-->
        <el-form-item label="机具SN码列表" prop="deliveryFee" v-if="form.brandId" wrap="soft|warp">
          <el-input v-model="form.snCodes" type="textarea" :autosize="{minRows: 6, maxRows: 4}" :style="{width: '100%'}">
          </el-input>
          <div style="font-size: 12px;"><span style="color: red;">*</span>请在每条SN码后换行继续输入</div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deliverOpen = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listTOrderInfo,
    getTOrderInfo,
    delTOrderInfo,
    addTOrderInfo,
    updateTOrderInfo,
    exportTOrderInfo,
    shipTOrder,
  } from "@/api/pos/TOrderInfo";
  import {
    listInfo,
  } from "@/api/pos/TShipperCodeInfo";

  export default {
    name: "TOrderInfo",
    dicts: ['orderType', 'orderStatus', 'deliveryType', 'payType'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单表格数据
        TOrderInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示发货弹出层
        deliverOpen: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderNo: null,
          userName: null,
          brand: null,
          specification: null,
          goodsName: null,
          goodsNum: null,
          orderType: "10",
          orderStatus: null,
          deliveryType: null,
          address: null,
          deliveryAmount: null,
          contactPerson: null,
          shopName: null,
          contactPhone: null,
          deliverGoodsTime: null,
          payType: null,
          payAmount: null,
          prepayTime: null,
          exchangeAmount: null,
          prepayInfo: null,
          payTime: null,
          deliveryFirm: null,
          oddNo: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          snCodes: [{
            required: true,
            message: '请填写Sn码',
            trigger: 'blur'
          }]
        },
        // tab页名称
        tabsNames: "buy",
        // 物流公司列表
        shipperCodeList: [],
        deliverLoading: false,
      };
    },
    created() {
      this.getList();
    },
    methods: {
      // tabs点击事件
      handleTabClick(tab, event) {
        if (tab.name == 'buy') {
          this.queryParams.orderType = "10";
        } else {
          this.queryParams.orderType = null;
        }
        this.getList();
      },
      /** 查询订单列表 */
      getList() {
        this.loading = true;
        listTOrderInfo(this.queryParams).then(response => {
          this.TOrderInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 物流列表
      getShipperCodeList(query) {
        this.deliverLoading = true;
        listInfo({
          shipperName: query
        }).then(res => {
          this.shipperCodeList = res.rows
          this.deliverLoading = false;
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          orderNo: null,
          userId: null,
          userName: null,
          brandId: null,
          brand: null,
          sepId: null,
          specification: null,
          goodsId: null,
          goodsName: null,
          goodsNum: null,
          orderType: null,
          orderStatus: "0",
          deliveryType: null,
          address: null,
          deliveryAmount: null,
          contactPerson: null,
          selfId: null,
          shopName: null,
          contactPhone: null,
          createTime: null,
          deliverGoodsTime: null,
          payType: null,
          payAmount: null,
          prepayTime: null,
          exchangeAmount: null,
          prepayInfo: null,
          payTime: null,
          deliveryFirm: null,
          oddNo: null,
          remark: null,
          snCodes: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加订单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getTOrderInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "订单详情";
        });
      },
      /* 发货 */
      handleDeliver(row) {
        this.reset();
        getTOrderInfo(row.id).then(response => {
          console.log(response)
          this.form = response.data;
          this.form.brandId = response.data.brandId
          this.form.orderStatus = "30";
          this.form.snCodes = response.data.snCodeList.toString();
          this.deliverOpen = true;
        });
        this.getShipperCodeList()
      },
      /** 提交按钮 */
      submitForm() {
        if (this.form.deliveryType == '1') {
          if ((!this.form.oddNo) || (!this.form.deliveryFirm)) {
            this.$message({
              message: '快递公司，快递单号为必填项',
              type: 'warning'
            });
            return;
          }
        }
        if (this.form.id != null) {
          this.form.snCodes = this.form.snCodes.replaceAll('，', ',')
          shipTOrder(this.form).then(response => {
            this.$modal.msgSuccess("发货成功");
            this.deliverOpen = false;
            this.getList();
          });
        }
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除？').then(function() {
          return delTOrderInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$modal.confirm('是否确认导出所有订单数据项？').then(() => {
          this.exportLoading = true;
          return exportTOrderInfo(queryParams);
        }).then(response => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
